package com.SessionTest;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


@WebServlet("/se1")
public class SessionDemo01 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //解决中文乱码
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html");

        //获得Session
        HttpSession session = req.getSession();

        //给Session 存东西
        session.setAttribute("name","苏文杰");

        //获取Session 的ID
        String sessionId = session.getId();

        //判断Session是否是新建
        if(session.isNew()){
            resp.getWriter().write("sessionID创建成功:"+sessionId);
        }else {
            resp.getWriter().write("session已经在服务器中存在了:"+sessionId);
        }

    }
}
